package com.uusee.crawler.util;

import org.apache.commons.lang.StringUtils;

public final class PathUtils {
	private static int TWO_GRADE = 2;
	
	public static String getThreeSubPathByMd5(String md5) {
		String one = md5.substring(0, 1);
		String two = md5.substring(1, 3);
		String three = md5.substring(3, 5);
		
		int oneOct = Integer.parseInt(one, 16);
		int twoOct = Integer.parseInt(two, 16);
		int threeOct = Integer.parseInt(three, 16);
		
		StringBuilder sb = new StringBuilder();
		sb.append(oneOct);
		sb.append("/");
		sb.append(twoOct);
		sb.append("/");
		sb.append(threeOct);
		sb.append("/");
		
		return sb.toString();
	}
	
	public static String getSubPathById(String id) {
		return getSubPathById(id,TWO_GRADE);
	}
	
	public static String getSubPathById(String id,int grade) {
		int numLen = grade*3;
		String idString = StringUtils.leftPad(id+"", numLen, '0');
		String sub1 = idString.substring(idString.length()-numLen,idString.length()-3);
		String sub2 = idString.substring(idString.length()-3);
		
		StringBuilder sb = new StringBuilder();
		sb.append(sub1);
		sb.append("/");
		sb.append(sub2);
		sb.append("/");
		
		return sb.toString();
	}
	
	public static String getContainIdSubPathById(String id) {
		return getContainIdSubPathById(id,TWO_GRADE);
	}
	
	public static String getContainIdSubPathById(String id,int grade) {
		int numLen = grade*3;
		String idString = StringUtils.leftPad(id+"", numLen, '0');
		String sub1 = idString.substring(idString.length()-numLen,idString.length()-3);
		String sub2 = idString.substring(idString.length()-3);
		
		StringBuilder sb = new StringBuilder();
		sb.append(sub1);
		sb.append("/");
		sb.append(sub2);
		sb.append("/");
		sb.append(id);
		sb.append("/");
		
		return sb.toString();
	}
}
